import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:kxy_flutter_app/auth.dart';
import 'package:kxy_flutter_app/components/loading_widget.dart';
import 'package:kxy_flutter_app/helper/http.dart';
import 'package:kxy_flutter_app/model/news_detail.dart';
import 'package:kxy_flutter_app/r.dart';
import 'package:kxy_flutter_app/services/integral_service.dart';
import 'package:kxy_flutter_app/view/news/content_news_widget.dart';
import 'package:kxy_flutter_app/view/news/video_news_widget.dart';

class NewsDetailView extends StatefulWidget {
  final String newsId;
  NewsDetailView({
    @required this.newsId,
  });

  @override
  State<StatefulWidget> createState() {
    return NewsDetailViewState();
  }
}

class NewsDetailViewState extends State<NewsDetailView> {
  NewsDetail newsDetail;
  bool isCollect;
  Future loadFuture;

  @override
  void initState() {
    IntegralService.addViewNews(widget.newsId);
    loadFuture = _loadNewsDetail();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text('新闻详情'),
        actions: [_createCollectButton()],
      ),
      body: FutureBuilder(
        future: loadFuture,
        builder: (context, snapshot) {
          Widget result;
          switch (snapshot.connectionState) {
            case ConnectionState.none:
            case ConnectionState.active:
            case ConnectionState.waiting:
              result = LoadingWidget();
              break;
            case ConnectionState.done:
              result = _createBody();
              break;
          }
          return result;
        },
      ),
    );
  }

  Widget _createBody() {
    if (newsDetail == null) {
      return Center(
        child: Text('新闻不存在'),
      );
    }
    if (newsDetail.contentType == NewsContentType.Content) {
      return ContentNewsWidget(newsDetail: newsDetail);
    }
    return VideoNewsWidget(newsDetail: newsDetail);
  }

  Widget _createCollectButton() {
    if (newsDetail == null) {
      return Container();
    }

    return IconButton(
      onPressed: () {
        isCollect ? cancelCollect() : collect();
      },
      icon: Image.asset(
        isCollect ? R.assetsCommonCollect : R.assetsCommonUncollect,
        height: 20,
      ),
    );
  }

  Future collect() async {
    var res = await Http.instance.post(
      '/news-app/coll/collNews',
      params: {
        'userId': Auth.instance.userId,
        'newsId': newsDetail.id,
      },
    );

    if (res.code == 200) {
      Fluttertoast.showToast(msg: '已收藏');
      setState(() {
        isCollect = true;
      });
    }
  }

  Future cancelCollect() async {
    var res = await Http.instance.post(
      '/news-app/coll/unCollNews',
      params: {
        'newsId': newsDetail.id,
        'userId': Auth.instance.userId,
      },
    );
    if (res.code == 200) {
      Fluttertoast.showToast(msg: '已取消收藏');
      setState(() {
        isCollect = false;
      });
    }
  }

  Future _loadNewsDetail() async {
    var res = await Http.instance.post(
      '/news-app/news/info',
      params: {
        'id': widget.newsId,
        'userId': Auth.instance.userId,
      },
    );

    if (res.code == 200) {
      var newsDetail = NewsDetail.fromJson(res.data);
      isCollect = res.data['collNews'];

      setState(() {
        this.newsDetail = newsDetail;
      });
    } else {
      Fluttertoast.showToast(msg: res.msg);
    }
  }
}
